﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bdxyit.com.RecruitmentAppSys.IDAL;
using bdxyit.com.RecruitmentAppSys.Models;
using System.Data.SqlClient;
using System.Data;
using bdxyit.com.RecruitmentAppSys.DBUtility;

namespace bdxyit.com.RecruitmentAppSys.SQLDAL
{
    public class OtherEligibilityInfoDAL : IOtherEligibilityInfoDAL
    {
        private const string SQL_SELECT_OTHERELIGIBILITYINFO = " select    ID,StartTime,EndTime,OtherID,OtherName,ProTecLevel,OtherNum,UserID from OtherEligibilityInfos ";
        private const string SQL_SELECT_OTHERELIGIBILITYINFO_BY_USERID = " select  top 1 ID,StartTime,EndTime,OtherID,OtherName,ProTecLevel,OtherNum,UserID from OtherEligibilityInfos "
                                                                        + "  where UserID=@UserID";
        private const string SQL_DELETE_OTHERELIGIBILITYINFO_BY_USERID = " delete from OtherEligibilityInfos "
                                                                        + "  where UserID=@UserID";
        private const string SQL_UPDATE_OTHERELIGIBILITYINFO_BY_USERID = " update OtherEligibilityInfos set "
                                                                        + " ID=@ID,"
                                                                        + " StartTime=@StartTime,"
                                                                        + " EndTime=@EndTime,"
                                                                        + " OtherID=@OtherID,"
                                                                        + " OtherName=@OtherName,"
                                                                        + " ProTecLevel=@ProTecLevel,"
                                                                        + " OtherNum=@OtherNum,"
                                                                        + " UserID=@UserID"
                                                                        + "  where UserID=@UserID";
        private const string SQL_INSERT_OTHERELIGIBILITYINFO = " insert into OtherEligibilityInfos("
                                                                + " ID,StartTime,EndTime,OtherID,OtherName,ProTecLevel,OtherNum,UserID)"
                                                                + "  values ("
                                                                + " @ID,@StartTime,@EndTime,@OtherID,@OtherName,@ProTecLevel,@OtherNum,@UserID)";
        private const string FIELD_ID = "@ID";
        private const string FIELD_StartTime = "@StartTime";
        private const string FIELD_EndTime = "@EndTime";
        private const string FIELD_OtherID = "@OtherID";
        private const string FIELD_OtherName = "@OtherName";
        private const string FIELD_ProTecLevel = "@ProTecLevel";
        private const string FIELD_OtherNum = "@OtherNum";
        private const string FIELD_UserID = "@UserID";
        public bool InsertOtherEligibilityInfo(OtherEligibilityInfo cmodel)
        {
            bool flag = false;
            SqlParameter[] OtherEligibilityInfoParms = GetOtherEligibilityInfoParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            OtherEligibilityInfoParms[0].Value = cmodel.ID;
            OtherEligibilityInfoParms[1].Value = cmodel.StartTime;
            OtherEligibilityInfoParms[2].Value = cmodel.EndTime;
            OtherEligibilityInfoParms[3].Value = cmodel.OtherID;
            OtherEligibilityInfoParms[4].Value = cmodel.OtherName;
            OtherEligibilityInfoParms[5].Value = cmodel.ProTecLevel;
            OtherEligibilityInfoParms[6].Value = cmodel.OtherNum;
            OtherEligibilityInfoParms[7].Value = cmodel.UserID;

            foreach (SqlParameter parm in OtherEligibilityInfoParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_INSERT_OTHERELIGIBILITYINFO;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool DeleteOtherEligibilityInfo(string UserID)
        {
            bool flag = false;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_UserID, SqlDbType.VarChar, 50);
            parm.Value = UserID;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_DELETE_OTHERELIGIBILITYINFO_BY_USERID;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool UpdateOtherEligibilityInfo(OtherEligibilityInfo cmodel)
        {
            bool flag = false;
            SqlParameter[] OtherEligibilityInfoParms = GetOtherEligibilityInfoParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            OtherEligibilityInfoParms[0].Value = cmodel.ID;
            OtherEligibilityInfoParms[1].Value = cmodel.StartTime;
            OtherEligibilityInfoParms[2].Value = cmodel.EndTime;
            OtherEligibilityInfoParms[3].Value = cmodel.OtherID;
            OtherEligibilityInfoParms[4].Value = cmodel.OtherName;
            OtherEligibilityInfoParms[5].Value = cmodel.ProTecLevel;
            OtherEligibilityInfoParms[6].Value = cmodel.OtherNum;
            OtherEligibilityInfoParms[7].Value = cmodel.UserID;

            foreach (SqlParameter parm in OtherEligibilityInfoParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_UPDATE_OTHERELIGIBILITYINFO_BY_USERID;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public OtherEligibilityInfo SelectOtherEligibilityInfoByUserID(string UserID)
        {
            OtherEligibilityInfo cmodel = null;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_UserID, SqlDbType.VarChar, 50);
            parm.Value = UserID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_OTHERELIGIBILITYINFO_BY_USERID, parm))
            {
                if (dr.Read())
                {
                    cmodel = new OtherEligibilityInfo();
                    if (dr.HasRows == true)
                    {
                        if (dr["ID"] != null && dr["ID"].ToString() != "")
                        {
                            cmodel.ID = int.Parse(dr["ID"].ToString());
                        }
                        if (dr["StartTime"] != null && dr["StartTime"].ToString() != "")
                        {
                            cmodel.StartTime = DateTime.Parse(dr["StartTime"].ToString());
                        }
                        if (dr["EndTime"] != null && dr["EndTime"].ToString() != "")
                        {
                            cmodel.EndTime = DateTime.Parse(dr["EndTime"].ToString());
                        }
                        if (dr["OtherID"] != null && dr["OtherID"].ToString() != "")
                        {
                            cmodel.OtherID = dr["OtherID"].ToString();
                        }
                        if (dr["OtherName"] != null && dr["OtherName"].ToString() != "")
                        {
                            cmodel.OtherName = dr["OtherName"].ToString();
                        }
                        if (dr["ProTecLevel"] != null && dr["ProTecLevel"].ToString() != "")
                        {
                            cmodel.ProTecLevel = dr["ProTecLevel"].ToString();
                        }
                        if (dr["OtherNum"] != null && dr["OtherNum"].ToString() != "")
                        {
                            cmodel.OtherNum = dr["OtherNum"].ToString();
                        }
                        if (dr["UserID"] != null && dr["UserID"].ToString() != "")
                        {
                            cmodel.UserID = dr["UserID"].ToString();
                        }
                    }
                }
            }
            return cmodel;
        }
        public List<OtherEligibilityInfo> SelectOtherEligibilityInfo()
        {
            List<OtherEligibilityInfo> selectlist = new List<OtherEligibilityInfo>();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_OTHERELIGIBILITYINFO))
            {
                while (dr.Read())
                {
                    OtherEligibilityInfo cmodel = new OtherEligibilityInfo();
                    //写添赋值语句
                    if (dr["ID"] != null && dr["ID"].ToString() != "")
                    {
                        cmodel.ID = int.Parse(dr["ID"].ToString());
                    }
                    if (dr["StartTime"] != null && dr["StartTime"].ToString() != "")
                    {
                        cmodel.StartTime = DateTime.Parse(dr["StartTime"].ToString());
                    }
                    if (dr["EndTime"] != null && dr["EndTime"].ToString() != "")
                    {
                        cmodel.EndTime = DateTime.Parse(dr["EndTime"].ToString());
                    }
                    if (dr["OtherID"] != null && dr["OtherID"].ToString() != "")
                    {
                        cmodel.OtherID = dr["OtherID"].ToString();
                    }
                    if (dr["OtherName"] != null && dr["OtherName"].ToString() != "")
                    {
                        cmodel.OtherName = dr["OtherName"].ToString();
                    }
                    if (dr["ProTecLevel"] != null && dr["ProTecLevel"].ToString() != "")
                    {
                        cmodel.ProTecLevel = dr["ProTecLevel"].ToString();
                    }
                    if (dr["OtherNum"] != null && dr["OtherNum"].ToString() != "")
                    {
                        cmodel.OtherNum = dr["OtherNum"].ToString();
                    }
                    if (dr["UserID"] != null && dr["UserID"].ToString() != "")
                    {
                        cmodel.UserID = dr["UserID"].ToString();
                    }
                    selectlist.Add(cmodel);
                }
            }
            return selectlist;
        }
        private SqlParameter[] GetOtherEligibilityInfoParameters()
        {
            SqlParameter[] parms = new SqlParameter[] {
new SqlParameter(FIELD_ID, SqlDbType.Int,4),
					new SqlParameter(FIELD_StartTime, SqlDbType.DateTime),
					new SqlParameter(FIELD_EndTime, SqlDbType.DateTime),
					new SqlParameter(FIELD_OtherID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_OtherName, SqlDbType.VarChar,100),
					new SqlParameter(FIELD_ProTecLevel, SqlDbType.VarChar,100),
					new SqlParameter(FIELD_OtherNum, SqlDbType.VarChar,100),
					new SqlParameter(FIELD_UserID, SqlDbType.VarChar,50)};
            return parms;
        }

    }
}